//
//  RefundInsureVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/5/13.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "RefundInsureVC.h"
#import "HelpViewController.h"

@interface RefundInsureVC ()
@property (weak, nonatomic) IBOutlet UILabel *jobNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *jobPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *payPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *refundPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *refundPriceDesLabel;
@property (weak, nonatomic) IBOutlet UITextField *givePriceTextField;

@end

@implementation RefundInsureVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"打赏／退款";
    self.jobNameLabel.text = self.model.positionName;
    self.jobPriceLabel.text = [NSString stringWithFormat:@"【%zdK-%zdK】",self.model.minSalary,self.model.maxSalary];
    self.orderNumberLabel.text = [NSString stringWithFormat:@"%@",self.model.orderNo];
    self.payPriceLabel.text = [NSString stringWithFormat:@"¥%.2f",self.model.price];
    self.refundPriceLabel.text = [NSString stringWithFormat:@"¥%.2f",self.model.refundAmount];
    self.refundPriceDesLabel.text = [NSString stringWithFormat:@"当前可退金额 %.2f元",self.model.refundAmount];
//    self.givePriceTextField.text = [NSString stringWithFormat:@"%.2f",self.model.refundAmount];
    self.givePriceTextField.text = @"0.00";
    [self.givePriceTextField addTarget:self action:@selector(priceValueChange:) forControlEvents:UIControlEventEditingChanged];
    [self rightButton];
}
-(void)rightButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 100, 44);
    [backBtn setImage:IMAGE(@"ic_problem") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)rightButtonClick{
    HelpViewController *vc = [[HelpViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)priceValueChange:(UITextField *)sender{
    if([self.givePriceTextField.text floatValue] > self.model.refundAmount){
        ALERT_VIEW(@"超出最大金额");
        return;
    }
    self.refundPriceDesLabel.text = [NSString stringWithFormat:@"当前可退金额 %.2f元",self.model.refundAmount - [self.givePriceTextField.text floatValue]];
}
- (IBAction)submitClick:(id)sender {
    if([self.givePriceTextField.text floatValue] > self.model.refundAmount){
        ALERT_VIEW(@"超出最大金额");
        return;
    }
    NSString *url = @"";
    NSString *key = @"";
    if (self.type == 0) {
        url = @"assurePass/finish";
        key = @"assurePassId";
    }else if (self.type == 1) {
        
    }else if (self.type == 2) {
        
    }
    
    NSMutableDictionary *dicInfoParams = [[NSMutableDictionary alloc]initWithObjectsAndKeys:
                                          [NSString stringWithFormat:@"%zd",self.model.assurePassId],key,
                                          self.payType,@"reasonType",
                                          self.des,@"remark",
                                          [NSString stringWithFormat:@"%f",self.model.refundAmount - [self.givePriceTextField.text floatValue]],@"refundAmount",
                                          [NSString stringWithFormat:@"%f",[self.givePriceTextField.text floatValue]],@"rewardAmount",
                                          @"111111",@"payPwd",
                                          nil];
    
    
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:dicInfoParams url:url result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
            ALERT_VIEW(@"订单交易完成") ;
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
